package org.NooLab.html.content;

import java.util.ArrayList;
import java.util.Vector;

import org.NooLab.utilities.strings.ArrUtilities;
import org.NooLab.utilities.strings.StringComparison;
import org.NooLab.utilities.strings.StringsUtil;
import org.apache.commons.lang3.StringUtils;

public abstract class TaggedContent {


	// =================================

	// object references ..............

	
	
	// main variables / properties ....

	protected String htmlString = "";
	
	String htmlSelection = "" ;
	
	protected boolean ignoreItalics = false;
	protected boolean ignoreUnderlined = false ;
	
	protected boolean includeEmbracingTags=true;
	
	// constants ......................

	
	
	// volatile variables .............

	protected String preparedString = "";

	
	
	// helper objects .................

	StringComparison strgcomp = new StringComparison();

	StringsUtil strgutil = new StringsUtil();
	
	
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
	public TaggedContent(){
		
		 
	}
	
	public TaggedContent( String htmlstr ){
		
		htmlString = htmlstr;
		
	}
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
	

	public String getCodeArea( String embracingTag, String markerTag ){
		
		String[] markertag = new String[1];
		markertag[0] = markerTag;
		
		return getCodeArea( embracingTag, markertag, 0) ;
	}

	public String getCodeArea( String embracingTag, String[] markerTags, int logicsMode){
		return getCodeArea( embracingTag, markerTags, null, logicsMode) ;
	}
	
	
	public String getCodeArea( String embracingTag, String[] markerTags, String[] markerContents, int logicsMode){
		String resultStr = "" ;
		
		String str, startTag, endTag ;
		int  p,p0,p1,p2 ;
		
		endTag = "</"+embracingTag+">" ;
		startTag = "<"+embracingTag ;
		
		p=-3;
		
		p1 = strgutil.indexOf( htmlString, markerTags) ;
		p  = strgutil.indexOf( htmlString, markerContents) ;
		
		if ((p1<0) && (p>=0)){
			p1=p;
		}
		if ((p1>0) && (p>p1)){
			p1=p;
		}
		p2 = p1-1 ;
		
		while (p1>0){
			
			p1 = strgutil.indexOfparticles( htmlString, markerTags, p2) ; 
			
			// preceding table tag
			if (p1>0){
				p0 = strgutil.indexOfBefore( htmlString, startTag, p1);
				/*
				if (p0>=0){
					p=p0;
				}else{
					p=p1;
				}  */
				p  = htmlString.indexOf( endTag, p0);
				
				if ((p0<p) && (p>p0)){
					p2 = htmlString.indexOf( endTag, p1);
					p  = htmlString.indexOf( startTag, p1);
					
					if ((p2>0) && 
						((p2<p) || (p<0))){
						p2 = p2 + endTag.length();
						str = htmlString.substring(p1,p2) ;
						
						if (str.length()>0){
							// str = "<table"+StringUtils.substringBetween( htmlString,"<"+embracingTag+" ","</"+embracingTag+">")+ "</"+embracingTag+">";
							
							if (includeEmbracingTags==false){
								// removing the first part  
							}
							resultStr = str;
							break ;
						}
					}
				}
			} // next h3 found ?
			else{
				
			}
			
		} // still any h3 found = p1>0 ??
		
		
		return resultStr ;
	}
	
	public String[] getCodeAreas( String embracingTag, String markerTag ){
		
		String[] markertag = new String[1];
		markertag[0] = markerTag;
		
		return getCodeAreas( embracingTag, markertag, null, 0) ;
	}
	
	
	
	 
	
	public String[] getCodeAreas( String embracingTag, String[] markerTags, String[] markerContents, int logicsMode){
		String[] resultStr = new String[0] ;
		ArrayList<String> results = new ArrayList<String>(); 
		String[] markertagsStart = new String[markerTags.length] ;
		
		String str, startTag, endTag ;
		int  p,p0,p1,p2 , nb,ne;
		
		startTag = "<"+embracingTag ;
		endTag = "</"+embracingTag+">" ;
		
		
		for (int i=0;i<markerTags.length;i++){
			markertagsStart[i] =  "<"+markerTags[i];
		}
		p=-3;
		
		p1 = strgutil.indexOf( preparedString, markertagsStart) ;
		p  = strgutil.indexOf( preparedString, markerContents) ;
		
		if ((p1<0) && (p>=0)){
			p1=p;
		}
		if ((p1>0) && (p>p1)){
			p1=p;
		}
		p2 = p1-1 ;
		
		while (p1>0){
			
			p1 = strgutil.indexOfparticles( preparedString, markerTags, p2) ; 
			
			// preceding table tag
			if (p1>0){
				p0 = strgutil.indexOfBefore( preparedString, startTag, p1);
				/*
				if (p0>=0){
					p=p0;
				}else{
					p=p1;
				}  */
				p  = preparedString.indexOf( endTag, p0);
				
				if ((p0<p) && (p>p0)){
					p2 = preparedString.indexOf( endTag, p1);
					p  = preparedString.indexOf( startTag, p1);
					
					// if ((p2>0) &&  ((p2<p) || (p<0)))
					{
						p2 = p2 + endTag.length();
						str = preparedString.substring(p0,p2) ;
						
						nb = strgutil.frequencyOfStr(str, "<table");
						ne = strgutil.frequencyOfStr(str, "</table>");
						
						if (nb>ne){
							p2 = preparedString.indexOf( endTag, p2+1);
							if (p2>0){
								p2 = p2 + endTag.length();
								str = preparedString.substring(p0,p2) ;
							}
						}
						results.add(str) ;
					}
				}
			} // next h3 found ?
			else{
				
			}
			
		} // still any h3 found = p1>0 ??
		
		// str = "<table"+StringUtils.substringBetween( preparedString,"<"+embracingTag+" ","</"+embracingTag+">")+ "</"+embracingTag+">";
		// String[]  <-  ArrayList<String>
		resultStr = (String[]) ArrUtilities.changeArraystyle(results) ;
			// .changeArrayStyle( results );
		/*
		resultStr = new String[results.size()] ;
		for (int i=0;i<results.size();i++){
			resultStr[i] = results.get(i) ;
		}
		*/
		return resultStr ;
	}
	
	
	/**
	 * 
	 * determining rows and columns as objects = collections
	 * 
	 */
	public abstract void parse();
	
	// 1-based position
	public int tagPosition( String htmlstr, String tag ){
		
		int pos=-1;
		String tagS, tagE, pstr; 
		int tn1, tn2 ;
		
		if (tag.contentEquals("<")==false){
			if (tag.contains("<")==false){
				tagS = "<"+tag;
				tagE = "</"+tag+">";
			}else{
				tagS = tag;
			}
		} else{
			tagS = "<" ;
		}
		
		tn1 = htmlstr.indexOf(tagS) ;
		if (tn1>0){
			pstr = htmlstr.substring(0,tn1-1) ;
			tn1 = strgutil.frequencyOfStr(pstr, "<");
		}
		pos=tn1+1 ;
			
		return pos;
	}
	
	public int tagCount( String str){
		int n;
		
		n = strgutil.frequencyOfStr( str, "<");
		
		return n;
	}
	
	public void setHtmlSelection() {

		htmlSelection = preparedString ;
	}
	
	// ------------------------------------------------------------------------
	
	public void setHtmlString( String str){
		htmlString = str;
	}

	public boolean isIgnoreItalics() {
		return ignoreItalics;
	}

	public void setIgnoreItalics(boolean ignoreItalics) {
		this.ignoreItalics = ignoreItalics;
	}

	public boolean isIgnoreUnderlined() {
		return ignoreUnderlined;
	}

	public void setIgnoreUnderlined(boolean ignoreUnderlined) {
		this.ignoreUnderlined = ignoreUnderlined;
	}

	public String getHtmlSelection() {
		
		return htmlSelection;
	}

	public boolean isIncludeEmbracingTags() {
		return includeEmbracingTags;
	}

	public void setIncludeEmbracingTags(boolean includeEmbracingTags) {
		this.includeEmbracingTags = includeEmbracingTags;
	}


	
	
}
